#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: liang kang
@contact: gangkanli1219@163.com
@desc: 
"""
import tensorflow as tf
from dltools.train.tools import load_model
from dltools.utils.log import get_console_logger


class Evaluator(object):

    def __init__(self, params, config=None, logger=None):
        self.params = params
        if logger is not None:
            self._logger = logger
        else:
            self._logger = get_console_logger('Evaluator')
        self.buffer = {}
        self.models = None
        self.config = config
        self.device = None
        self._config()
        self._load_model()

    def _config(self):
        """
        生成配置文件和设备

        Returns
        -------

        """
        if self.config is None:
            if self.params['cpu_only']:
                self.config = tf.ConfigProto(device_count={'CPU': 1})
                self.device = '/cpu:0'
            else:
                self.config = tf.ConfigProto(device_count={'GPU': 1})
                self.device = '/gpu:0'
        else:
            device_count = self.config.device_count
            if device_count is None:
                if self.params['cpu_only']:
                    self.config.device_count = {'CPU': 1}
                    self.device = '/cpu:0'
                else:
                    self.config.device_count = {'GPU': 1}
                    self.device = '/gpu:0'

    def _load_model(self):
        """
        载入模型

        Returns
        -------

        """
        self.models = load_model(self.params['model_dir'])

    def _detect(self, *args, **kwargs):
        """
        检测实例

        Parameters
        ----------
        args
        kwargs

        Returns
        -------

        """
        raise NotImplementedError

    def _update(self, *args, **kwargs):
        """
        执行全面检测

        Parameters
        ----------
        args
        kwargs

        Returns
        -------

        """
        raise NotImplementedError

    def update(self, *args, **kwargs):
        assert len(args) + len(kwargs) > 0
        self._logger.info('Starting Detecting ...')
        self._update(*args, **kwargs)
